package pl.edu.utp.kursy.pdf;

import java.text.SimpleDateFormat;
import java.util.Date;
import pl.edu.utp.kursy.database.Company;
import pl.edu.utp.kursy.database.Academy;
import pl.edu.utp.kursy.database.CourseEvent;
import pl.edu.utp.kursy.database.Worker;

public final class InoviceFields {
    
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    private static double priceNettoTotal;
    private static double valueNettoTotal;
    private static double amountVatTotal;
    private static double amountBruttoTotal;
    
    private Company company;
    private Academy academy;
    
    public InoviceFields(Company company, Academy academy){
        this.company = company;
        this.academy = academy;
        
        priceNettoTotal = 0.0;
        valueNettoTotal = 0.0;
        amountVatTotal  = 0.0;
        amountBruttoTotal = 0.0;
    }
    
    public Date getIssueDate(){
        return new Date();
    }
    
    public String getInoviceNumber(){
        //return new SimpleDateFormat("yyyy/MMdd/HHmmss").format(new Date());
        return "";
    }
    
    public String getSellerAddress(){
        return academy.getAddress();
    }
    
    public String getSellerName(){
        return academy.getName();
    }
    
    public String getSellerNip(){
        return academy.getNip();
    }
    
    public String getCustomerAddress(){
        return company.getAddress();
    }
    
    public String getCustomerName(){
        return company.getName();
    }
    
    public String getCustomerNip(){
        return company.getNip();
    }
    
    public Date getSellDate(){
        return new Date();
    }
    
    public double getTotalPriceNetto(){
        return priceNettoTotal;
    }

    public double getTotalValueNetto(){
        return valueNettoTotal;
    }
    
    public double getTotalVatValue(){
        return amountVatTotal;
    }
    
    public double getTotalValueBrutto(){
        return amountBruttoTotal;
    }
    
    public String getPriceInWords(double price){
        return NumberSpeaker.procKwotaSlownie(price);
    }
    
    public String getTotalPriceInWords(){
        return getPriceInWords(amountBruttoTotal);
    }
    
    public int getNumOfWorkers(CourseEvent course){
        int workersAmount = 0;
        for(Worker w: company.getWorkers()){
            for(CourseEvent ce: w.getCourseEvents()){
                if(ce.getId() == course.getId()){
                    workersAmount++;
                    break;
                }
            }
        }
        return workersAmount;
    }
    
    public double getCoursePriceNetto(CourseEvent courseEvent){
        return courseEvent.getCost();
    }
    
    public double getCourseValueNetto(CourseEvent courseEvent){
        return getCoursePriceNetto(courseEvent) * getNumOfWorkers(courseEvent);
    }
    
    public double getCourseVatValue(CourseEvent courseEvent){
        return (getCourseValueNetto(courseEvent)*courseEvent.getVat())/100;
    }
    
    public double getCourseValueBrutto(CourseEvent courseEvent){
        return getCourseVatValue(courseEvent) + getCourseValueNetto(courseEvent);
    }
    
    public void calculatePrices(CourseEvent courseEvent){

        double priceNetto = getCoursePriceNetto(courseEvent);
        double valueNetto = getCourseValueNetto(courseEvent);
        double vatValue =   getCourseVatValue(courseEvent);
        double amountBrutto = valueNetto + vatValue;

        priceNettoTotal = priceNettoTotal + priceNetto;
        valueNettoTotal = valueNettoTotal + valueNetto;
        amountVatTotal = amountVatTotal + vatValue;
        amountBruttoTotal = amountBruttoTotal + amountBrutto;  
    }
}